import React, {Component} from "react";
import DetailHead from '../components/DetailHead';
import Table from "../../../component/Table";
import Search from "../../../component/Search";
import {formatTime} from "@/utils";

class HomeworkDetail extends Component{

    state = {
        searchConfig: {
            columns: [
                {
                    type: 'Input',
                    name: 'studentName',
                    placeholder: '学生姓名',
                },
                {
                    type: 'Select',
                    name: 'isCorrect',
                    placeholder: '批改状态',
                    options: [
                        {value: '已批改', text: '已批改'},
                        {value: '未批改', text: '未批改'}
                    ]
                },
            ]
        },
        tableConfig: {
            columns: [
                {
                    title: '#',
                    width: 60,
                    fixed: 'left',
                    render: (text, record, index) => {
                        const {current,pageSize} = this.state.tableConfig.pagination;
                        return (current-1) * pageSize + (index+1)
                    }
                },
                {
                    title: '学生',
                    dataIndex: 'studentName',
                    width: 90
                },
                {
                    title: '学号',
                    dataIndex: 'studentCode',
                    width: 100
                },
                {
                    title: '学校',
                    dataIndex: 'schoolName',
                    width: 120
                },
                {
                    title: '所在班级',
                    dataIndex: 'className',
                    width: 120
                },
                {
                    title: '提交时间',
                    render: (text) => `${formatTime(text.submitTime, 16)}`,
                    width: 140
                },
                {
                    title: '是否提交',
                    dataIndex: 'taskStatus',
                    width: 80
                },
                {
                    title: '批改状态',
                    dataIndex: 'isCorrect',
                    width: 80
                },
                {
                    title: '批改人',
                    dataIndex: 'correctUserName',
                    width: 90
                },
                {
                    title: '得分',
                    dataIndex: 'score',
                    width: 100
                },
                {
                    title: '标签',
                    render: text => {
                        var label = '';
                        if(text.endorse){
                            var labelObj = JSON.parse(text.endorse);
                            label = Object.keys(labelObj).toString()
                        }
                        return label
                    },
                    width: 120
                },
                {
                    title: '评语',
                    dataIndex: 'comment',
                    width: 120
                }
            ],
            pagination: {
                current: 1,
                pageSize: 15
            },
            params: {
                paramsMap: {
                    taskId: this.props.match.params.id
                },
                sortName: "score",
                sortType: "desc"
            },
            request: {
                method: 'post',
                url: '/TaskInfo/getTaskInfoList',
                headers: {}
            }
        }
    };

    render() {
        return (
            <div className="table-detail">
                <DetailHead />
                <Search config={this.state.searchConfig} search={this.search} />
                <Table config={this.state.tableConfig} onRef={(ref) => this.table = ref } />
            </div>
        )
    }

    // 回调
    search = (searchConfig) => {
        const { tableConfig } = this.state;
        this.setState({
            tableConfig: {
                ...tableConfig,
                pagination: {
                    current: 1,
                    pageSize: 15
                }
            }
        }, () => {
            const { params, pagination } = this.state.tableConfig;
            this.table.getTableList({...params, paramsMap: {...params['paramsMap'],...searchConfig}}, pagination)
        })
    }
}

export default HomeworkDetail;